package com.fpj.sat.vehicle_administrator.util;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.ooxml.JRDocxExporter;

public class Reporte {
	
	private static final Logger logger = Logger.getLogger(Reporte.class);
	
	/** this method will call the report from data source*/
    public static JasperPrint generateReport(InputStream reportStream, Map<String, Object> parameters) {
        try {
            JasperPrint jasperPrint = null;
            if (parameters == null) {
            	parameters = new HashMap<String, Object>();
            }
            try {
                /**You can also test this line if you want to display 
                 * report from any absolute path other than the project root path*/
                //jasperPrint = JasperFillManager.fillReport("F:/testreport/"+reportName+".jasper",hm, con);
                jasperPrint = JasperFillManager.fillReport(reportStream, parameters, Util.getConnectionDS());
            } catch (JRException e) {
                logger.error(e);
            }
            return jasperPrint;
        } catch (Exception ex) {
        	logger.error(ex);
            return null;
        }
 
    }
    
    /** this method will call the report from data source*/
    public static void generateReport(InputStream reportStream, OutputStream fileStream, Map<String, Object> parameters) {
        try {
        	JasperPrint jasperPrint = null;
            if (parameters == null) {
            	parameters = new HashMap<String, Object>();
            }
            try {
                /**You can also test this line if you want to display 
                 * report from any absolute path other than the project root path*/
                //jasperPrint = JasperFillManager.fillReport("F:/testreport/"+reportName+".jasper",hm, con);
            	jasperPrint = JasperFillManager.fillReport(reportStream, parameters, Util.getConnectionDS());
                JRDocxExporter ex = new JRDocxExporter();
                ex.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
                ex.setParameter(JRExporterParameter.OUTPUT_STREAM, fileStream);
                ex.exportReport();
            } catch (JRException e) {
            	logger.error(e);
            }
        } catch (Exception ex) {
        	logger.error(ex);
        }
 
    }
    
    /** call this method when your report has an empty data source*/
    public static JasperPrint generateEmptyDataSourceReport(InputStream reportStream, Map<String, Object> parameters) {
        try {
            JasperPrint jasperPrint = null;
            if (parameters == null) {
                parameters = new HashMap<String, Object>();
            }
            try {
                jasperPrint = JasperFillManager.fillReport(reportStream, parameters, new JREmptyDataSource());
            } catch (JRException e) {
            	logger.error(e);
            }
            return jasperPrint;
        } catch (Exception ex) {
        	logger.error(ex);
            return null;
        }
 
    }

}
